<?php 
/**
 * Datepicker Element.
 * @param field Nome do Campo
 * @param label Label
 * @param style Estilo
 * @param empty Exibe campo vazio 
 * Ex. de utilizacao:
 * 			echo $this->element('datepicker', array(
				'field' => 'data_contrato',
				'label' => 'Data do Contrato',
				'style' => 'width:30%'))
 */
if(!isset($label)){
	$label = '';
}
if(!isset($style)){
	$style = '';
}
if(!isset($empty)){
	$empty = false;
}

if(!isset($value) || empty($value) || $value==' '){
	$value = $this->Form->value($field);
}

$id = $field.'_calendar';
if(strchr($id, '.') ){
	$id = str_replace('.', '_', $id)	;
}

	
$webroot = ($this->webroot == '/')?$this->webroot.'webroot/':$this->webroot;

echo $this->Form->hidden($field);
echo $this->Form->input($id, array(
				'label' => $label,
				'style'=> $style,
				'id'=>$id,
				'readonly'=>'true'));

$this->Html->scriptStart();
?>
$(function() {
	$('#<?php echo $id?>').datepicker({
		showOn: 'button',
		buttonImage: '<?php echo "{$webroot}img/calendar-small.gif";?>',
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		altField: '#<?php echo $this->Html->domId($field)?>', altFormat: 'yy-mm-dd',
		<?php if(!$empty  && empty($value) || $value==' '):?>
		onRender : $('#<?php echo $this->Html->domId($field)?>').val($.datepicker.formatDate('yy-mm-dd', new Date()))
		<?php endif;?>	
	});
	
	<?php if (!$empty):?>
		<?php if (!empty($value) && $value!=' '):?>
			$('#<?php echo $id?>').val("<?php echo date('d/m/Y', strtotime($value));?>");
		<?php else:?>
			$('#<?php echo $id?>').val($.datepicker.formatDate('dd/mm/yy', new Date()));	
		<?php endif;?>
	<?php else:?>
		<?php if (!empty($value) && $value!=' '):?>
			$('#<?php echo $id?>').val("<?php echo date('d/m/Y', strtotime($value));?>");			
		<?php endif;?>	
	<?php endif;?>		
});
<?php 
echo  $this->Html->scriptEnd();

//echo $js->writeBuffer();
?>